package com.beordie.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Description 文件上传的工具类
 * @Date 2021/8/6 10:04
 * @Created 30500
 */
public class UploadFileUtils {
    public static String infoPath = "E:\\file/java learn/bill_manager/travel_manager/src/main/resources/static/img/imageInfo/";
    public static String bigPath = "E:\\file/java learn/bill_manager/travel_manager/src/main/resources/static/img/imageBig/";
    public static String smallPath = "E:\\file/java learn/bill_manager/travel_manager/src/main/resources/static/img/imageSmall/";
    public static String relativelyInfoPath = "img/imageInfo/";
    public static String relativelyBigPath = "img/imageBig/";
    public static String relativelySmallPath = "img/imageSmall/";

    static {
        File file = new File(infoPath);
        if (file.exists())
            file.mkdirs();
        file = new File(bigPath);
        if (file.exists())
            file.mkdirs();
        file = new File(smallPath);
        if (file.exists())
            file.mkdirs();
    }

    public static String getFileName(int opera, MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String type = originalFilename.substring(originalFilename.lastIndexOf('.'));
        String realName = UUID.randomUUID().toString().replaceAll("-", "");
        String realPath = null;
        String fileName = null;
        switch (opera) {
            case 1:
                realPath = infoPath;
                fileName = relativelyInfoPath + realName + type;
                break;
            case 2:
                realPath = bigPath;
                fileName = relativelyBigPath + realName + type;
                break;
            case 3:
                realPath = smallPath;
                fileName = relativelySmallPath + realName + type;
                break;
        }
        file.transferTo(new File(realPath + realName + type));
        return fileName;
    }

}
